﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;

using Telerik.Cms.Engine;
using Telerik.News;

namespace Telerik.Sitefinity.Services.Migration
{
	public partial class MigrationExportService : IMigrationExportService
	{
		public Message GetNewsList()
		{
			List<MigrationItemContentDefinition> output = new List<MigrationItemContentDefinition>();

			foreach (KeyValuePair<string, ContentProviderBase> provider in m_AllProviders)
			{
				// Check if provider is DefaultNewsProvider
				if (provider.Value is Telerik.News.Data.DefaultNewsProvider)
				{
					string providerName = provider.Key;
					NewsManager newsManager = new NewsManager(providerName);

					if (provider.Value.AllowLocalization)
					{
						foreach (var culture in m_Cultures)
						{
							System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(culture);

							IList newsItems = newsManager.Content.GetContent();
							AddGenericContentGuidToList(newsItems, output, providerName, culture);
						}
					}
					else
					{
						IList newsItems = newsManager.Content.GetContent();
						AddGenericContentGuidToList(newsItems, output, providerName, String.Empty);
					}
				}
			}

			MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
			return Message.CreateMessage(ver, "GetNewsListResponse", output);
		}

		public Message GetSingleNews(Message newsDefinitionParam)
		{
			MigrationItemContentDefinition param = newsDefinitionParam.GetBody<MigrationItemContentDefinition>();

			NewsManager newsManager = new NewsManager(param.ProviderName);
			if (!String.IsNullOrEmpty(param.Language))
				System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(param.Language);
			IContent news = newsManager.Content.GetContent(param.Id);

			MigrationGenericContent result = new MigrationGenericContent();

			GetSingleGenericContentItem(result, news, param.ProviderName, param.Language);

			MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
			return Message.CreateMessage(ver, "GetSingleNewsResponse", result);
		}
	}
}
